<template>
  <div>
    <!-- 搜索结果页-头部导航 -->
    <div class="search-result-container">
      <!-- 点击实现后退效果 -->
      <van-nav-bar
        title="搜索结果"
        left-arrow
        @click-left="$router.go(-1)"
        fixed
      />
    </div>
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="秃驴肚子里的墨水用完了!"
      loading-text="阿狗玩命加载中..."
      @load="onLoad"
      offset="50"
      :immediate-check="false"
      ><ArticleItem
        v-for="(item, index) in articlesList"
        :key="item.art_id"
        :obj="item"
        :index="index"
        :show="false"
      />
    </van-list>
  </div>
</template>

<script>
import ArticleItem from '../Layout/Home/components/articleItem.vue'
import { resultLIst } from '@/api/search.js'
// import { getArticlesList } from '@/api/home.js'
export default {
  name: 'SearchResult',
  components: { ArticleItem },
  data () {
    return {
      articlesList: [],
      loading: false,
      finished: false,
      page: 0

    }
  },
  computed: {
    txt () {
      return this.$route.params.keywords
    }
  },
  methods: {
    async onLoad () {
      if (this.articlesList.length > 0) {
        this.page++
        const resp = await resultLIst(this.txt, this.page)
        console.log(resp)
        // 判断是否有数据
        if (resp.data.results.length === 0) {
          this.finished = true
          return
        }
        this.articlesList = [...this.articlesList, ...resp.data.results]
      }

      this.loading = false
    }
  },
  async activated () {
    this.page++
    const resp = await resultLIst(this.txt, this.page)
    this.articlesList = resp.data.results
    console.log(resp)
  },
  deactivated () {
    this.articlesList = []
    this.page = 0
  }
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
